package com.yuexun.beilunpatient.ui.test.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.test.adapter.TestListAdapter;
import com.yuexun.beilunpatient.ui.test.bean.AssayRow;
import com.yuexun.beilunpatient.ui.test.bean.AssayRowSet;
import com.yuexun.beilunpatient.ui.test.model.impl.TestModel;
import com.yuexun.beilunpatient.ui.test.presenter.impl.TestListPresenter;
import com.yuexun.beilunpatient.ui.test.ui.view.ITestListView;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/30 0030.
 */

public class Act_Test_List extends BaseKJActivity implements ITestListView {

    @Bind(R.id.list)
    PullToRefreshList pullList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private TestListPresenter presenter;
    ListView listView;
    TestListAdapter adapter;
    private int nowpage = 1;
    private int pagesize = 20;
    ArrayList<AssayRow> assayResps = new ArrayList<>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_test);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new TestListPresenter(this, new TestModel());

        initList();
        ApiGetData();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiGetData();
            }
        });

        listView = pullList.getRefreshView();
        listView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        pullList.setPullRefreshEnabled(true);
        pullList.setPullLoadEnabled(true);
        ((FooterLoadingLayout) pullList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new TestListAdapter(listView, assayResps, R.layout.item_testlist);
        listView.setAdapter(adapter);

        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiGetData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiGetData();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AssayRow item = (AssayRow) adapterView.getItemAtPosition(i);
                if (item.getApply_no() != null && !item.getApply_no().isEmpty()) {
                    Bundle bundle = new Bundle();
                    bundle.putString("rpeportNo", item.getReport_no() + "");
                    bundle.putString("hospId", item.getHosp_id());
                    Intent intent = new Intent(aty, Act_Test_Detail.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    ViewInject.toast("未出报告单!");
                }
            }
        });
    }

    /**
     * 请请求数据
     */
    private void ApiGetData() {
        if (SystemTool.checkNet(aty)) {
            Map<String, String> params = new HashMap<>();
            params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
//            params.put("cardNo", "431122201006040165");
//            params.put("endDate", "2018-09-01");
//            params.put("startDate", "2018-05-01");
            params.put("cardNo", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
//            params.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//            params.put("startDate", new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getDateBefore(new Date(), 3)));
            params.put("pageIndex", nowpage + "");
            params.put("pageSize", pagesize + "");
            presenter.inquireAssayPageList(params);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showTestList(BaseEntity<AssayRowSet> bean) {
        if (pullList.isPullRefreshEnabled()) {
            pullList.onPullDownRefreshComplete();
        }
        if (bean.getStatus().equals("200")) {
            if(bean.getDatas().get(0).getCode() == 1){
                if (bean.getDatas().get(0).getRows().size() == 0) {
                    pullList.setHasMoreData(false);
                } else {
                    if (pullList.isPullLoadEnabled()) {
                        pullList.onPullUpRefreshComplete();
                    }
                }

                if (nowpage == 1) {
                    assayResps.clear();
                    assayResps = bean.getDatas().get(0).getRows();
                } else {
                    assayResps.addAll(bean.getDatas().get(0).getRows());
                }
                adapter.refresh(assayResps);
            }else {
                ViewInject.toast(bean.getDatas().get(0).getDesc());
            }

        }else {
            ViewInject.toast(bean.getMessage());
        }

        if (assayResps.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
